/*
 * SettingsWidget.cpp
 *
 *  Created on: Jan 5, 2011
 *      Author: Kevin Mueller
 */

#include "SettingsWidget.h"

#include "MainWindow.h"

#include "../logic/Main.h"
#include "../logic/Settings.h"

#include <QtGui>

SliderLabel::SliderLabel(AbstractSetting *s, QWidget *parent) :
	QLabel(parent) {
	setting = s;
	setIntText(s->getCurrent());
}

void SliderLabel::setIntText(int i) {
	setText(tr(setting->getDescription(i)));
}

/**********************************************************/

SettingsComboBox::SettingsComboBox(AbstractSetting *s, QWidget *parent) :
	QComboBox(parent) {
	setting = s;
	for (int i = s->getMinimum(); i < s->getMaximum(); i++) {
		this->addItem(s->tr(s->getDescription(i)));
	}
	this->setCurrentIndex(s->getCurrent());
}

void SettingsComboBox::retranslateUi() {
	for (int i = setting->getMinimum(); i < setting->getMaximum(); i++) {
		this->setItemText(i, setting->tr(setting->getDescription(i)));
	}
	this->resize(this->sizeHint());
}

/**********************************************************/

SettingsWidget::SettingsWidget(MainWindow *parent) :
	QDialog(static_cast<QWidget*> (parent)) {

	// Transparent background
	//this->setAttribute(Qt::WA_NoSystemBackground);
	//setAttribute(Qt::WA_DeleteOnClose, true);

	createGeneral();
	createGPS();
	createRouting();
	createGUI();

	QHBoxLayout *mainLayout = new QHBoxLayout();

	tabs = new QTabWidget();
	tabs->addTab(general, "");
	tabs->addTab(gps, "");
	tabs->addTab(routing, "");
	tabs->addTab(gui, "");
	tabs->setTabsClosable(false);
	tabs->setMovable(false);
	tabs->setTabPosition(QTabWidget::North);

	//	QPushButton *ok = new QPushButton(tr("Save"), this);
	bClose = new QPushButton(this);
	connect(bClose, SIGNAL(clicked()), this, SLOT(close()));

	QVBoxLayout *buttons = new QVBoxLayout();
	buttons->addStretch();
	//	buttons->addWidget(ok);
	buttons->addWidget(bClose);

	mainLayout->addWidget(tabs, 8);
	mainLayout->addLayout(buttons, 1);

	retranslateUi();
	connect(parent, SIGNAL(retranslateRequest()), this, SLOT(retranslateUi()));

	this->setLayout(mainLayout);
}

SettingsComboBox* SettingsWidget::createCombo(AbstractSetting *s) {
	SettingsComboBox *box = new SettingsComboBox(s);
	connect(box, SIGNAL(currentIndexChanged(int)), s, SLOT(setCurrent(int)));
	return box;
}

QAbstractSlider* SettingsWidget::createScrollBar(AbstractSetting *s) {
	QAbstractSlider *bar = new QSlider(Qt::Horizontal);
	bar->setMinimum(s->getMinimum());
	bar->setMaximum(s->getMaximum());
	bar->setValue(s->getCurrent());
	connect(bar, SIGNAL(valueChanged(int)), s, SLOT(setCurrent(int)));
	//	bar->resize(bar->sizeHint());
	//	bar->resize(100, 80);

	return bar;
}

QCheckBox* SettingsWidget::createCheckbox(AbstractSetting *s) {
	QCheckBox *box = new QCheckBox();
	box->setCheckState(s->getCurrent() ? Qt::Checked : Qt::Unchecked);
	connect(box, SIGNAL(stateChanged(int)), s, SLOT(setCurrent(int)));
	return box;
}

void SettingsWidget::createGeneral() {
	QFormLayout *layout = new QFormLayout();
	//	layout->addWidget(new QLabel(tr("General"), this));

	Settings& s = Main::getInstance().getSettings();

	language = createCombo(s.getLanguage());
	connect(language, SIGNAL(currentIndexChanged(int)), parent(), SLOT(switchLanguage()));
	orientation = createCombo(s.getOrientation());
	unblank = createCombo(s.getUnblank());
	unit = createCombo(s.getUnit());
	degree = createCombo(s.getDegreeFormat());

	lLanguage = new QLabel("xxxxxxxxxxxxxxxxxxxx");
	lOrientation = new QLabel();
	lUnblank = new QLabel();
	lUnit = new QLabel();
	lDegreeFormat = new QLabel();

	layout->addRow(lLanguage, language);
	layout->addRow(lOrientation, orientation);
	layout->addRow(lUnblank, unblank);
	layout->addRow(lUnit, unit);
	layout->addRow(lDegreeFormat, degree);

	QWidget *tmp = new QWidget();
	tmp->setLayout(layout);

	general = new QScrollArea();
	general->setWidget(tmp);
}

void SettingsWidget::createGPS() {
	QFormLayout *layout = new QFormLayout();
	//	layout->addWidget(new QLabel(tr("GPS"), this));

	Settings& s = Main::getInstance().getSettings();

	AbstractSetting *p = s.getPollingInterval();
	QAbstractSlider *polling = createScrollBar(p);
	SliderLabel* lPolling = new SliderLabel(p);
	connect(polling, SIGNAL(valueChanged(int)), lPolling, SLOT(setIntText(int)));

	QHBoxLayout *pollingLayout = new QHBoxLayout();
	pollingLayout->addWidget(polling);
	pollingLayout->addWidget(lPolling);
	//	polling->setEditText(tr("Polling Interval"));

	AbstractSetting *r = s.getReducedPollingInterval();
	QAbstractSlider *reducedPolling = createScrollBar(r);
	SliderLabel* lReducedPolling = new SliderLabel(r);
	connect(reducedPolling, SIGNAL(valueChanged(int)), lReducedPolling, SLOT(setIntText(int)));
	//	reducedpolling->setEditText(tr("Reduced Polling During Off-Screen"));

	QHBoxLayout *reducedPollingLayout = new QHBoxLayout();
	reducedPollingLayout->addWidget(reducedPolling);
	reducedPollingLayout->addWidget(lReducedPolling);

	autocenter = createCombo(s.getAutoCenter());
	speedlimit = createCombo(s.getSpeedLimit());

	lGPSPolling = new QLabel("xxxxxxxxxxxxxxxxxxxx");
	lReducedGPSPolling = new QLabel();
	lAutoCenter = new QLabel();
	lSpeedLimit = new QLabel();

	layout->addRow(lGPSPolling, pollingLayout);
	layout->addRow(lReducedGPSPolling, reducedPollingLayout);
	layout->addRow(lAutoCenter, autocenter);
	layout->addRow(lSpeedLimit, speedlimit);

	QWidget *tmp = new QWidget();
	tmp->setLayout(layout);

	gps = new QScrollArea();
	gps->setWidget(tmp);
}

void SettingsWidget::createRouting() {
	QFormLayout *layout = new QFormLayout();
	//	layout->addWidget(new QLabel(tr("Routing"), this));

	Settings& s = Main::getInstance().getSettings();

	announcement = createCombo(s.getAnnouncement());
	routecalc = createCombo(s.getRouteCalculation());

	avoidHighway = createCheckbox(s.getAvoidHighways());
	avoidTollroad = createCheckbox(s.getAvoidTollroads());

	lAnnouncements = new QLabel("xxxxxxxxxxxxxxxxxxxx");
	lRouteCalculation = new QLabel();

	layout->addRow(lAnnouncements, announcement);
	layout->addRow(lRouteCalculation, routecalc);
	layout->addWidget(avoidHighway);
	layout->addWidget(avoidTollroad);

	QWidget *tmp = new QWidget();
	tmp->setLayout(layout);

	routing = new QScrollArea();
	routing->setWidget(tmp);
}

void SettingsWidget::createGUI() {
	QFormLayout *layout = new QFormLayout();
	//	layout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
	//	layout->addWidget(new QLabel(tr("GUI"), this));

	Settings& s = Main::getInstance().getSettings();

	colors = createCombo(s.getColors());
	arrival = createCombo(s.getArrival());

	AbstractSetting *r = s.getShadowMapSize();
	QAbstractSlider *shadow = createScrollBar(r);
	SliderLabel* lShadow = new SliderLabel(r);
	connect(shadow, SIGNAL(valueChanged(int)), lShadow, SLOT(setIntText(int)));
	QHBoxLayout *shadowMapLayout = new QHBoxLayout();
	shadowMapLayout->addWidget(shadow);
	shadowMapLayout->addWidget(lShadow);

	rotate = createCheckbox(s.getRotateMap());

	scale = createCheckbox(s.getShowScale());
	compass = createCheckbox(s.getShowCompass());
	poi = createCheckbox(s.getShowPOI());
	velocity = createCheckbox(s.getShowVelocityVector());

	volumeKeys = createCombo(s.getVolumeKeys());
	connect(volumeKeys, SIGNAL(currentIndexChanged(int)), parent(), SLOT(volumeKeysGrabber()));
	//	QCheckBox *gpsstatus = new QCheckBox(tr("Show GPS status"), this);

	lColors = new QLabel("xxxxxxxxxxxxxxxxxxxx");
	lShadowMap = new QLabel();
	lArrival = new QLabel();
	lVolumeKeys = new QLabel();

	layout->addRow(lColors, colors);
	layout->addRow(lArrival, arrival);
	layout->addRow(lVolumeKeys, volumeKeys);
	layout->addRow(lShadowMap, shadowMapLayout);
	layout->addWidget(rotate);
	layout->addWidget(scale);
	layout->addWidget(compass);
	layout->addWidget(poi);
	layout->addWidget(velocity);
	//	layout->addWidget(gpsstatus);

	QWidget *tmp = new QWidget();
	tmp->setLayout(layout);

	gui = new QScrollArea();
	gui->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
	gui->setWidget(tmp);
}

void SettingsWidget::retranslateUi() {
	setWindowTitle(tr("Preferences"));

	tabs->setTabText(0, tr("General"));
	tabs->setTabText(1, tr("GPS"));
	tabs->setTabText(2, tr("Routing"));
	tabs->setTabText(3, tr("GUI"));

	bClose->setText(tr("Close"));

	language->retranslateUi();
	orientation->retranslateUi();
	unblank->retranslateUi();
	unit->retranslateUi();
	degree->retranslateUi();

	lLanguage->setText(tr("Language:"));
	lOrientation->setText(tr("Orientation:"));
	lUnblank->setText(tr("Unblank screen:"));
	lUnit->setText(tr("Units:"));
	lDegreeFormat->setText(tr("Degree format:"));

	autocenter->retranslateUi();
	speedlimit->retranslateUi();

	lGPSPolling->setText(tr("GPS polling interval:"));
	lReducedGPSPolling->setText(tr("Reduced GPS polling:"));
	lAutoCenter->setText(tr("Auto center:"));
	lSpeedLimit->setText(tr("Speed limit:"));

	announcement->retranslateUi();
	routecalc->retranslateUi();

	lAnnouncements->setText(tr("Announcements:"));
	lRouteCalculation->setText(tr("Route calculation:"));
	avoidHighway->setText(tr("Avoid highways"));
	avoidTollroad->setText(tr("Avoid tollroads"));

	lShadowMap->setText(tr("Shadow map size:"));

	rotate->setText(tr("Rotate map"));
	scale->setText(tr("Show scale"));
	compass->setText(tr("Show compass"));
	poi->setText(tr("Show POI"));
	velocity->setText(tr("Show velocity-vector"));

	colors->retranslateUi();
	arrival->retranslateUi();
	volumeKeys->retranslateUi();

	lColors->setText(tr("Colors:"));
	lArrival->setText(tr("Arrival:"));
	lVolumeKeys->setText(tr("Volume-Keys:"));
}

void SettingsWidget::closeEvent(QCloseEvent *event) {
	Main::getInstance().getSettings().save();

	event->accept();

	/*if (maybeSave()) {
	 writeSettings();
	 event->accept();
	 } else {
	 event->ignore();
	 }*/
}

